﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMN.Models;
using CMN.Entity;
using CMN.Common;
using CMN.Config;
using CMN.Models.Interfaces;
using System.Text.RegularExpressions;
namespace CMN.Forms
{
    public partial class frmAddSite : Form
    {
        public int Id = 0;
        public frmAddSite()
        {
            InitializeComponent();
        }
        public frmAddSite(int id)
        {
            InitializeComponent();
            Id = id;
           
            
           
            
        }
        private void frmAddSite_Load(object sender, EventArgs e)
        {
            IDataService<SiteInfoEntity> SiteInfoSv = ServiceBuilder.BuildSiteInfoService();
            SiteInfoEntity site = SiteInfoSv.Look(Id);
            if (site != null)
            {
                tbBR.Text = site.BR.ToString();
                tbIndex.Text = site.IndexNum.ToString();
                tbUserName.Text = site.LoginName;
                tbMark.Text = site.Mark;
                tbPR.Text = site.PR.ToString();
                cmboxType.Text = site.SendUrlType;
                tbSiteName.Text = site.SiteName;
                tbUrl.Text = site.SiteUrl;
                cmboxSiteType.Text = site.SiteType;
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbUrl.Text))
            {
                MessageBox.Show("请填写网站URL");
                return;
            }
            SiteInfoEntity site = new SiteInfoEntity();
            site.SiteUrl = tbUrl.Text;
            IDataService<SiteInfoEntity> SiteInfoSv = ServiceBuilder.BuildSiteInfoService();
            if (Id == 0)
            {
                int recordNum = 0;
                QueryParam qp = new QueryParam();
                qp.Where = " siteUrl='" + site.SiteUrl + "'";
                List<SiteInfoEntity> siteList = SiteInfoSv.List(qp, out recordNum);
                if (siteList.Count > 0)
                {
                    MessageBox.Show("该网址已添加！");
                    return;
                }
            }


            site.BR = Utils.StringToInt(tbBR.Text, 0);
            site.IndexNum = tbIndex.Text;
            site.LoginName = tbUserName.Text;
            site.Mark = tbMark.Text;
            site.PR = Utils.StringToInt(tbPR.Text, 0);
            site.SendUrlType = cmboxType.Text;
            site.SiteType = cmboxSiteType.Text;
            site.SiteName = tbSiteName.Text;

            if (Id == 0)
                SiteInfoSv.Insert(site);
            else
            {
                site.Id = Id;
                SiteInfoSv.Update(site);

            }
            clear();
            //List<SiteInfoEntity> sys_moduleList = SiteInfoSv.List(qp, out recordNum);
        }
        private void clear()
        {
            tbBR.Text = "";
            tbIndex.Text = "";
            tbMark.Text = "";
            tbPR.Text = "";
            tbSiteName.Text = "";
            tbUrl.Text = "";
            tbUserName.Text = "";
        }

        private void btnGetInfo_Click(object sender, EventArgs e)
        {
            SiteInfoEntity site = new SiteInfoEntity();
            site = GetSiteInfo(tbUrl.Text);
            try
            {
                Uri baseUri = new Uri(tbUrl.Text);
                tbUrl.Text = site.SiteUrl;

                tbIndex.Text = site.IndexNum.ToString();

                tbPR.Text = site.PR.ToString();
                tbBR.Text = site.BR.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        public SiteInfoEntity GetSiteInfo(string url)
        {
            SiteInfoEntity site = new SiteInfoEntity();
           
            try
            {
                Uri baseUri = new Uri(url);
                site.SiteUrl = "http://" + baseUri.Authority.ToLower();
                string strContent = Utils.GetRequestString("http://www.baidu.com/s?wd=site%3A" + baseUri.Authority, 16000, 1, Encoding.GetEncoding("utf-8"), null);
               string index = Utils.GetCutString(strContent, "该网站共有", "个网页被百度收录", false);
                if(index=="")
                    index = Utils.GetCutString(strContent, "找到相关结果数约", "个", false);
               site.IndexNum = index.Replace(",", "");
               
                strContent = Utils.GetRequestString("http://seo.chinaz.com/?host=" + baseUri.Authority, 16000, 1, Encoding.GetEncoding("utf-8"), "http://seo.chinaz.com/");
               string strPR = Utils.GetCutString(strContent, "/template/default/images/ranks/Rank_", ".gif", false);
               if (strPR.Trim() == "-2")
                   strPR = "0";
               site.PR = Utils.StringToInt(strPR, 0);
               string strBR = Utils.GetCutString(strContent, "/template/default/images/baiduapp/", ".gif", false);
               site.BR = Utils.StringToInt(strBR, 0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return site;
           
        }


       
    }
}
